package com.granada.model.common;

import com.granada.common.Curd;
import com.granada.entity.Ao;
import com.granada.entity.Page;
import com.granada.entity.SysPermission;
import com.granada.model.common.base.PermissionBase;
import com.granada.utils.MapperBeanUtils;

import java.util.Date;

public abstract  class Permission<T extends SysPermission,F extends Page> extends PermissionBase<T,F> implements Curd<T> {
    public T vo;
    public F fo;
    public Ao ao;

    public Permission(T vo,F fo, Ao ao) {
        this.vo = vo;
        this.fo = fo;
        this.ao = ao;
    }

    public Long add() throws Exception {
        ao.id = this.addPermissionBefore();
        this.addPermissionAfter();
        return null;
    }

    public  void update() throws Exception {
        this.updatePermissionBefore();
        this.updatePermissionAfter();
    }

    public void delete() throws Exception {
        this.deletePermissionBefore();
        this.deletePermissionAfter();
    }

    public Long addPermissionBefore() throws Exception{
        SysPermission sysPermission = SysPermission.builder()
                .code(vo.getCode())
                .name(vo.getName())
                .perms(vo.getPerms())
                .url(vo.getUrl())
                .method(vo.getMethod())
                .pid(vo.getPid())
                .orderNum(vo.getOrderNum())
                .type(vo.getType())
                .status(vo.getStatus())
                .createTime(new Date())
                .updateTime(vo.getUpdateTime())
                .identity(vo.getIdentity())
                .testId(0)
                .icon(vo.getIcon())
                .build();
        MapperBeanUtils.getSysPermissionMapper().insert(sysPermission);
        return sysPermission.getId();
    }

    public abstract void addPermissionAfter() throws Exception;

    public abstract void updatePermissionBefore() throws Exception;
    public abstract void updatePermissionAfter() throws Exception;

    public abstract void deletePermissionBefore() throws Exception;
    public abstract void deletePermissionAfter() throws Exception;

}
